package dao.impl;

/**
 * Created by IntelliJ IDEA.
 * User: winstars
 * Date: 2006-11-7
 * Time: 13:59:29
 * To change this template use File | Settings | File Templates.
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;

public class ConnectionPool {

    private String dbUserName;
    private String dbPassword;
    private String connectionURL;
    private int maxConnection;
    private Vector<Connection> freeConnections;
    private int checkedOut;

    public ConnectionPool(String DriverName,String userName, String password, String url, int maxConn) {
        this.dbUserName = userName;
        this.dbPassword = password;
        this.connectionURL = url;
        this.maxConnection = maxConn;
        freeConnections = new Vector<Connection>();
        try {
			Class.forName(DriverName).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    }// initalize

    public synchronized Connection getConnection() {
        Connection conn = null;
        if (freeConnections.size() > 0) {
            conn = (Connection) freeConnections.elementAt(0);
            freeConnections.removeElementAt(0);
            try {
                if (conn.isClosed()) {
                    conn = getConnection();
                }
            } catch (SQLException ex) {
                conn = getConnection();
            }
        } else if (maxConnection == 0 || checkedOut < maxConnection) {
            conn = newConnection();
        }
        if (conn != null) {
            checkedOut++;
        }
        return (conn);
    }// using FIFO method to get connection instance

    public synchronized Connection getConnection(long timeout) {
        long startTime = new java.util.Date().getTime();
        Connection conn;
        while ((conn = getConnection()) == null) {
            try {
                wait(timeout);
            } catch (InterruptedException ex) {
            }
            if (new java.util.Date().getTime() - startTime >= timeout) {
                return (null);
            }
        }
        return conn;
    }

    public Connection newConnection() {
        Connection conn = null;
        try {
            if (dbUserName == null) {
                conn = DriverManager.getConnection(connectionURL);
            } else {
                conn = DriverManager.getConnection(connectionURL, dbUserName, dbPassword);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (conn);
    }

    public synchronized void freeConnection(Connection conn) {
        freeConnections.addElement(conn);
        checkedOut--;
        notifyAll();
    }

    public synchronized void release() {
        Enumeration allConnections = freeConnections.elements();
        while (allConnections.hasMoreElements()) {
            try {
                Connection conn = (Connection) allConnections.nextElement();
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        freeConnections.removeAllElements();
    }

}
